package com.smz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.TLaserDAO;
import com.smz.dao.TProductDAO;
import com.smz.dao.TProductOrderDAO;
import com.smz.entity.TLaser;
import com.smz.entity.TPrinter;
import com.smz.entity.TProductOrder;
import com.smz.service.TLaserService;
import com.smz.service.TPrinterService;
import com.smz.service.TProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class TProductOrderServiceImpl extends ServiceImpl<TProductOrderDAO, TProductOrder> implements TProductOrderService {
    @Autowired
    private TProductOrderDAO tProductOrderDAO;
    @Autowired
    TLaserService tLaserService;
    @Autowired
    TPrinterService tPrinterService;
    @Autowired
    TLaserDAO tLaserDAO;
    @Autowired
    TProductDAO tProductDAO;



    @Override
    public Long selectReadyProduct() {
        QueryWrapper<TProductOrder> queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_status","未生产");
        Long count = tProductOrderDAO.selectCount(queryWrapper);
        return count;
    }

    @Override
    public Page<TProductOrder> findAllOrderByPage(Integer current, Long size) {
        //新建分页
        Page<TProductOrder> page = new Page<>(current, size);
        // 返回分页结果
        return page.setRecords(tProductOrderDAO.selectProductEnterprise(page));
    }

    @Override
    public List<TProductOrder> findOrderDesign(Integer orderId) {
        return tProductOrderDAO.selectProductDesign(orderId);
    }

    @Override
    public Page<TProductOrder> findDoneProductByPage(Integer current, Long pageSize, Long orderType) {
        Page<TProductOrder> orderPage = new Page<>(current, pageSize);
        //如果是订单状态是字符串形式
        String typeStr=null;
        switch (String.valueOf(orderType)) {
            case "1": {
                typeStr = "待排单";
                break;
            }
            case "2": {
                typeStr = "待生产";
                break;
            }
            case "3": {
                typeStr = "生产中";
                break;
            }
            case "4": {
                typeStr = "已入库";
                break;
            }
            case "5": {
                typeStr = "已结束";
                break;
            }
        }

        List<TProductOrder> tProductOrders = tProductOrderDAO.selectDoneOrReady(orderPage, String.valueOf(orderType), typeStr);
        return orderPage.setRecords(tProductOrders);
    }

    @Transactional
    @Override
    public String updateOrder(TProductOrder tProductOrder) {
        //更改订单信息
        UpdateWrapper<TProductOrder> wrapper = new UpdateWrapper<>();
        wrapper.eq("order_id",tProductOrder.getOrderId()).set("product_status","2");
        tProductOrderDAO.update(tProductOrder,wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("l_no",tProductOrder.getOrderId());
        List<TLaser> tLasers = tLaserDAO.selectByMap(map);
        if (tLasers.size()==0){
            //增加激光表信息
            TLaser tLaser = new TLaser();
            tLaser.setLNo(String.valueOf(tProductOrder.getOrderId()));
            tLaser.setLStatus("2");
            tLaser.setLPrinter(tProductOrder.getPrinter());
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("l_no",String.valueOf(tProductOrder.getOrderId()));
            tLaserService.saveOrUpdate(tLaser,updateWrapper);
        }
        //改变打印机的责任生产线
        TPrinter tPrinter = new TPrinter();
        tPrinter.setPLine(tProductOrder.getProductLineId());
        UpdateWrapper<TPrinter> printerWrapper = new UpdateWrapper<>();
        printerWrapper.eq("p_num",tProductOrder.getPrinter());
        tPrinterService.saveOrUpdate(tPrinter,printerWrapper);
        return "ok";
    }

    @Override
    public List<TProductOrder> findAllOrders() {
        return tProductOrderDAO.selectAllOrders();
    }

    @Override
    public TProductOrder findOrderById(Integer id) {
        return tProductOrderDAO.selectOrderById(id);
    }

    @Override
    public Page<TProductOrder> findPageTProductOrder(Integer current, Integer rows) {
        Long count = tProductOrderDAO.selectCount(new QueryWrapper<TProductOrder>().lambda().eq(TProductOrder::getOrderStatus, "-2"));
        Page<TProductOrder> data = tProductOrderDAO.selectPage(new Page<>(current,rows),null);
        List<TProductOrder> tProductOrderList = tProductOrderDAO.selectTProductOrderGgy((current - 1) * rows, rows);
        data.setRecords(tProductOrderList);
        data.setTotal(count);
        return data;
    }

    @Override
    public Page<TProductOrder> findTProductOrderById(Integer current, Integer rows, Integer id) {
        Long count = tProductOrderDAO.selectCount(null);
        Page<TProductOrder> page = new Page<>(current, rows, count);
        if(id == null){
            Page<TProductOrder> tProductOrderPage = tProductOrderDAO.selectPage(page, null);
            tProductOrderPage.getRecords().forEach(aa->{
                aa.setTProduct(tProductDAO.selectTProduct(aa.getProductId()));
            });
            return tProductOrderPage;
        }else {
            Page<TProductOrder> tProductOrderPage = tProductOrderDAO.selectPage(page, new QueryWrapper<TProductOrder>().lambda().like(TProductOrder::getOrderId, id));
            tProductOrderPage.getRecords().forEach(aa->{
                aa.setTProduct(tProductDAO.selectTProduct(aa.getProductId()));
            });
            return tProductOrderPage;
        }

    }
    @Override
    public List<TProductOrder> findProOrderLikeOrderId(String orderId) {
        return tProductOrderDAO.selectProOrderLikeOrderId(orderId);
    }
}
